<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function truncate(str, maxlength) {
        if (str.length >= maxlength) {
          // 如果超过限制，截取允许的最大字符个数
          return str.substr(0, maxlength) + "...";
        }
        return str;
      }
      console.log(truncate("this is best for everyone", 10)); // this is best ...
      console.log(truncate("this is best for everyone", 15)); // this is best for  ...
      console.log(truncate("this is best for everyone", 150));
    </script>
  </body>
</html>
